<?php

namespace app\adminmini\service;

use app\adminmini\model\Userlevel;
use think\facade\Db;

class UserlevelService extends BaseService
{
    public function __construct()
    {
        $this->model = new Userlevel();
    }

    public function getList()
    {
        $data = request()->param();
        // 查询条件
        $map = [];

        if (isset($data['add_time'])) {
            if ($data['add_time'] != "") {
                $add_time = strtotime($data['add_time']);
                $map[] = ['o.add_time', '>=', $add_time];
            }

        }
        if (isset($data['end_time'])) {
            if ($data['add_time'] != "") {
                $add_time = strtotime($data['end_time']) + 86400;
                $map[] = ['o.end_time', '<=', $add_time];
            }

        }
        // 名称
        $name = isset($data['name']) ? trim($data['name']) : '';
        if ($name) {
            $map[] = ['c.name', 'like', "%{$name}%"];
        }
        $map[] = ['o.mark', '=', 1];
        $map[] = ['o.status', '=', 1];
        $map[] = ['o.store_id', '=', $this->userId()];
        if (isset($data['mobile'])) {
            if ($data['mobile'] != "") {
                $moblie = ($data['mobile']);
                $map[] = ['u.mobile', '=', $moblie];
            }

        }
        if (isset($data['nickname'])) {
            if ($data['nickname'] != "") {
                $nickname = $data['nickname'];
                $map[] = ['u.nickname', 'like', "%" . $nickname . "%"];
            }

        }

        $list = Db::name('customer_level_order')
            ->alias('o')
            ->join('customer_member_level c', 'c.id=o.level_id')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile')
            ->where($map)->order('create_time desc')->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $info) {
            $level = Db::name('customer_member_level')->where('id', $info['level_id'])->find();
            $info['name'] = $level['name'];
            $list[$i] = $info;
        }
        $count = Db::name('customer_level_order')
            ->alias('o')
            ->join('customer_member_level c', 'c.id=o.level_id')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile')
            ->where($map)->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}